package com.jg.servlet;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.jg.entity.Department;
import com.jg.service.DepartmentService;
import com.jg.service.EmployeeProfilesService;
import com.jg.service.impl.DepartmentServiceImpl;
import com.jg.service.impl.EmployeeProfilesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/4/21 上午10:35
 */
@WebServlet("/department")
public class DepartmentServlet extends HttpServlet {
    private DepartmentService departmentService = new DepartmentServiceImpl();
    private EmployeeProfilesService employeeService = new EmployeeProfilesServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //允许所有来源访问
        resp.addHeader("Access-Control-Allow-Origin", "*");
        //允许请求的方法
        resp.addHeader("Access-Control-Allow-Methods", "POST, GET");
        String method = req.getParameter("method")==null?"":req.getParameter("method");
        switch (method){
            case "list":
                list(req,resp);
                break;
            default:
                break;
        }
    }

//    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String empId = req.getParameter("empId");
//        String empName = req.getParameter("empName");
//        String deptId = req.getParameter("departmentId");
//        String position = req.getParameter("position");
//        String startTime = req.getParameter("hireDate");
//        String phone = req.getParameter("phone");
//        Part photo = req.getPart("photo");
//        String photoUrl = FileUtil.getImgUrl(req,photo);
//        Employee employee = new Employee();
//        if (empId == null){
//            employee.setId(0);
//        }else {
//            employee.setId(Integer.parseInt(empId));
//        }
//        employee.setEmpName(empName);
//        employee.setDepId(Integer.parseInt(deptId));
//        employee.setPosition(position);
//        employee.setStartDate(DateUtil.parseDate(startTime));
//        employee.setPhone(phone);
//        employee.setImgUrl(photoUrl);
//        int result = employeeService.edit(employee);
//        Map<String,String> map = new HashMap<>();
//        if (result > 0){
//            map.put("code","200");
//            map.put("msg","员工信息修改成功");
//        }else{
//            map.put("code","500");
//            map.put("msg","员工信息修改失败");
//        }
//    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Department> allDepartments = departmentService.getAllDepartments();
        String jsonString = JSON.toJSONString(allDepartments, JSONWriter.Feature.WriteMapNullValue);
        resp.getWriter().write(jsonString);
    }
}
